/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.article.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.article.entity.TestArticles;
import com.thinkgem.jeesite.modules.article.service.TestArticlesService;

/**
 * 文章管理Controller
 * @author Tony
 * @version 2018-05-11
 */
@Controller
@RequestMapping(value = "${adminPath}/article/testArticles")
public class TestArticlesController extends BaseController {

    @Autowired
    private TestArticlesService testArticlesService;

    @ModelAttribute
    public TestArticles get(@RequestParam(required = false) String id) {
        TestArticles entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = testArticlesService.get(id);
        }
        if (entity == null) {
            entity = new TestArticles();
        }
        return entity;
    }

    @RequiresPermissions("article:testArticles:view")
    @RequestMapping(value = { "list", "" })
    public String list(TestArticles testArticles, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<TestArticles> page = testArticlesService.findPage(new Page<TestArticles>(request, response), testArticles);
        model.addAttribute("page", page);
        return "modules/article/testArticlesList";
    }

    @RequiresPermissions("article:testArticles:view")
    @RequestMapping(value = "form")
    public String form(TestArticles testArticles, Model model) {
        model.addAttribute("testArticles", testArticles);
        return "modules/article/testArticlesForm";
    }

    @RequiresPermissions("article:testArticles:edit")
    @RequestMapping(value = "save")
    public String save(TestArticles testArticles, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, testArticles)) {
            return form(testArticles, model);
        }
        testArticlesService.save(testArticles);
        addMessage(redirectAttributes, "保存文章成功");
        return "redirect:" + Global.getAdminPath() + "/article/testArticles/?repage";
    }

    @RequiresPermissions("article:testArticles:edit")
    @RequestMapping(value = "delete")
    public String delete(TestArticles testArticles, RedirectAttributes redirectAttributes) {
        testArticlesService.delete(testArticles);
        addMessage(redirectAttributes, "删除文章成功");
        return "redirect:" + Global.getAdminPath() + "/article/testArticles/?repage";
    }

}